/*
 * 路由分发
 */
var url = require("url")
var router_config = {
	"/api/complex/Base": require("./router/complex/base"),
	"/api/complex/StoreMapFactory": require("./router/complex/StoreMapFactory"),
	"/api/complex/ClientStoreMapFactory": require("./router/complex/ClientStoreMapFactory"),
	"/api/complex/StoreFromMapFactory": require("./router/complex/StoreFromMapFactory"),
	"/api/complex/ExpressInterface": require("./router/complex/ExpressInterface"),
	"/api/complex/WechatClientService": require("./router/complex/WechatClientService"),
}

var _router_error = function(req, res) {
		res.send(404, "404 no find.");
		console.error("----404----");
		console.log(req.route);
		console.error("----404----");
	}
	// 打印数据库接口
var router = function(app) {
	app.use(function(req, res, next) {
		if (req.headers.referer) {
			var origin_info = url.parse(req.headers.referer);
			var origin_url = (origin_info.protocol || "http:") + "//" + origin_info.host;
		} else if (req.headers.origin) {
			var origin_url = req.headers.origin;
		} else {
			origin_url = "*"
		}
		res.header("Access-Control-Allow-Origin", origin_url);
		// res.header("Access-Control-Allow-Headers", "X-Requested-With");
		res.header("Access-Control-Allow-Headers", "X-PINGOTHER, Set-Cookie, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version");
		res.header("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE,OPTIONS");
		res.header("Access-Control-Allow-Credentials", true);
		res.header("P3P", "CP=IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA");
		next();
	});
	for (var i in router_config) {
		(function(router_key) {
			var router_object = router_config[router_key];
			Object.keys(router_object).forEach(function(method) {
				var router_foo = router_object[method];
				method = method.toLowerCase();
				app[method](router_key, router_foo);
			});
			console.log("路由器初始化配置接口：", router_key);
		}(i));
	};
};
module.exports = router;